<template>
  <div class="medical-reimbursement">
    <h2>{{ $t('Paybaoxiao_medical_reimbursement') }}</h2>
    
    <!-- 搜索框 -->
    <div class="search-container">
      <input type="text" v-model="searchText" :placeholder="$t('Paybaoxiao_search_placeholder')" class="search-input" />
      <button @click="performSearch" class="search-button">{{ $t('Paybaoxiao_search_button') }}</button>
    </div>
    
    <div class="reimbursement-table">
      <table>
        <thead>
          <tr>
            <th>{{ $t('Paybaoxiao_disease') }}</th>
            <th>{{ $t('Paybaoxiao_ratio') }}</th>
            <th>{{ $t('Paybaoxiao_total_cost') }}</th>
            <th>{{ $t('Paybaoxiao_payment_time') }}</th>
            <th>{{ $t('Paybaoxiao_actions') }}</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(row, index) in filteredReimbursementData" :key="index">
            <td>
              <input type="text" v-model="row.disease" class="form-input" />
            </td>
            <td>
              <input type="number" v-model="row.ratio" class="form-input" />
            </td>
            <td>
              <input type="number" v-model="row.totalCost" class="form-input" />
            </td>
            <td>
              <input type="datetime-local" v-model="row.paymentTime" class="form-input" />
            </td>
            <td>
              <button @click="deleteRow(index)" class="delete-button">{{ $t('Paybaoxiao_delete_row') }}</button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    
    <div class="button-group">
      <button @click="addRow" class="add-button">{{ $t('Paybaoxiao_add_row') }}</button>
      <button @click="saveData" class="save-button">{{ $t('Paybaoxiao_save_data') }}</button>
    </div>
    
    <!-- 数据统计 -->
    <div class="statistics">
      <p>{{ $t('Paybaoxiao_total_rows', { count: reimbursementData.length }) }}</p>
      <p>{{ $t('Paybaoxiao_total_cost_sum', { sum: totalCostSum }) }}</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      reimbursementData: [
        { disease: '高血压', ratio: 80, totalCost: 500, paymentTime: this.getCurrentDateTime() },
        { disease: '糖尿病', ratio: 70, totalCost: 300, paymentTime: this.getCurrentDateTime() },
        { disease: '冠心病', ratio: 75, totalCost: 800, paymentTime: this.getCurrentDateTime() }
      ],
      searchText: ''
    };
  },
  computed: {
    filteredReimbursementData() {
      return this.reimbursementData.filter(row =>
        row.disease.includes(this.searchText)
      );
    },
    averageRatio() {
      if (this.reimbursementData.length === 0) return 0;
      const sum = this.reimbursementData.reduce((acc, curr) => acc + (curr.ratio || 0), 0);
      return (sum / this.reimbursementData.length).toFixed(2);
    },
    totalCostSum() {
  const sum = this.reimbursementData.reduce((sum, row) => sum + (parseFloat(row.totalCost) || 0), 0);
  console.log('Total cost sum:', sum); // 调试输出
  return sum;
}
  },
  methods: {
    addRow() {
      this.reimbursementData.push({ 
        disease: '', 
        ratio: '', 
        totalCost: 0, 
        paymentTime: this.getCurrentDateTime() 
      });
    },
    deleteRow(index) {
      if (this.reimbursementData.length > 1) {
        this.reimbursementData.splice(index, 1);
      }
    },
    performSearch() {
      // 这里可以添加搜索逻辑，比如高亮显示匹配的行等
    },
    saveData() {
      alert(this.$t('Paybaoxiao_data_saved'));
    },
    getCurrentDateTime() {
      const now = new Date();
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, '0');
      const day = String(now.getDate()).padStart(2, '0');
      const hours = String(now.getHours()).padStart(2, '0');
      const minutes = String(now.getMinutes()).padStart(2, '0');
      return `${year}-${month}-${day}T${hours}:${minutes}`;
    }
  }
};
</script>

<style>

.medical-reimbursement {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Arial', sans-serif;
}

h2 {
  text-align: center;
  color: #333;
  margin-bottom: 30px;
}

.search-container {
  display: flex;
  margin-bottom: 20px;
}

.search-input {
  flex: 1;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
}

.search-button {
  padding: 10px 20px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-left: 10px;
}

.search-button:hover {
  background-color: #45a049;
}

.reimbursement-table {
  background-color: #ffffff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th {
  background-color: #f8f9fa;
  text-align: left;
  padding: 12px;
  border-bottom: 1px solid #ddd;
  font-weight: bold;
}

td {
  padding: 12px;
  border-bottom: 1px solid #ddd;
}

.form-input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin-bottom: 5px;
}

.delete-button {
  background-color: #f44336;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
}

.delete-button:hover {
  background-color: #d32f2f;
}

.button-group {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  gap: 15px;
}

.add-button, .save-button {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.add-button {
  background-color: #2196F3;
  color: white;
}

.add-button:hover {
  background-color: #0b7dda;
}

.save-button {
  background-color: #2196F3;
  color: white;
}

.save-button:hover {
  background-color: #0b7dda;
}

.statistics {
  margin-top: 30px;
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 8px;
  text-align: center;
}

.statistics p {
  margin: 5px 0;
  font-size: 14px;
  color: #666;
}
.form-input[type="datetime-local"] {
  padding: 7px;
}
</style>